import math

'''
 CollisionHandler class
 deals with collision of player entities with other players and bullets
'''
class CollisionHandler:
    def __init__(self):
        pass
    
    '''
     update(List,Player,List)
     Do actul checks
     @param enemies: list of enemy objects who may collide with things
     @param player: Player object who may collide with things
     @param bullets: list of bullets which may hit players
    '''
    def update(self,enemies,player,bullets):
        #See if any of the enemies are colliding into each other or with the player
        for e in enemies:
            dist = ((e.xpos - player.xpos)**2 + (e.ypos - player.ypos)**2)**.5
            #If their distance is less than the hitradius, collide
            if dist < (player.hitradius+e.hitradius)*2:
                #swap velocities
                vx = e.vx
                vy = e.vy
                e.vx = player.vx
                e.vy = player.vy
                player.vx = vx
                player.vy = vy
                
                #make sure we arent inside of each other
                x =(e.xpos - player.xpos)
                y =(e.ypos - player.ypos)
                theta = math.atan2(-y, -x)
                player.xpos += math.sin(theta)*2
                player.ypos += math.cos(theta)*2
                e.xpos -= math.sin(theta)*2
                e.ypos -= math.cos(theta)*2
                
            for en in enemies:
                if e!=en:
                    dist = (math.fabs((en.xpos - e.xpos)**2 + (en.ypos - e.ypos)**2))**.5
                    if dist < (e.hitradius+en.hitradius)*2:
                        vx = en.vx
                        vy = en.vy
                        en.vx = e.vx
                        en.vy = e.vy
                        e.vx = vx
                        e.vy = vy
                        x =(en.xpos - e.xpos)
                        y =(en.ypos - e.ypos)
                        theta = math.atan2(-y, -x)
                        e.xpos += math.sin(theta)*5
                        e.ypos += math.cos(theta)*5
        
        #see if any of the bullets are colliding with people
        for b in bullets:
            if b.pid!=player.pid:
                dist = (math.fabs((b.xpos - player.xpos)**2 + (b.ypos - player.ypos)**2))**.5
                if dist<player.hitradius*2:
                    
                    #figure out their health/shield situation if they've been hit
                    if player.shield>0:
                        if player.shield-5>0:
                            player.shield-=5
                        else:
                            player.shield=0
                            player.health+=player.shield-5
                            if player.health<=0:
                                player.newDeaths.append(b.pid)
                                print player.newDeaths
                    else:
                        player.health-=5
                        if player.health<=0:
                            player.newDeaths.append(b.pid)
                            print player.newDeaths
                    #recycle the bullet so it doesnt hit anyone else
                    b.recycle=True
                    break
                
            #Check the bullet against enemies now
            for e in enemies:
                if b.pid!=e.pid:
                    dist = (math.fabs((b.xpos - e.xpos)**2 + (b.ypos - e.ypos)**2))**.5
                    if dist<e.hitradius*2:
                        if e.shield>0:
                            if e.shield-5>0:
                                e.shield-=5
                            else:
                                e.shield=0
                                e.health+=e.shield-5
                        else:
                            e.health-=5
                        b.recycle = True
                        break     
                
                
